﻿using FlickrNet;
using Lsk.Business;
using Lsk.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MigrationTool
{
    public partial class Form1 : Form
    {
        //string pathSource = @"C:\Users\skhor\Lsk\lsksys\REM_TEST\property";
        string pathSource = ConfigurationManager.AppSettings["Path"].ToString();
        string pathAvatarSource = ConfigurationManager.AppSettings["AvatarPath"].ToString();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Process and then  close form
            ProcessSyncFlickr();
            this.Close();
              
        }

        private void button1_Click(object sender, EventArgs e)
        {
             
            lskEntities  _entity = new lskEntities();
            AW_Picture photo = (from aw in _entity.AW_Picture where aw.Picture == "2f3cf3fa-be77-488b-9e10-3597316936a8.jpg" select aw).FirstOrDefault();
            PhotoManager a = new PhotoManager();
           
            string  photoPath = pathSource + "\\" + photo.Picture;
            FileStream fileStream = new FileStream(photoPath, FileMode.Open, FileAccess.Read);
            a.UploadAsync(fileStream, photo);
           

        }

        private void ProcessSyncFlickr()
        {
               
            string photoPath;
            StringBuilder sb = new StringBuilder();

            Logger.log(sb.Append("=============== Finished processing ===================").ToString());

            PhotoManager a = new PhotoManager();
            List<AW_Picture> data = a.GetListPhoto().ToList();
            

            foreach (AW_Picture photo in data)
            {
                sb.Append("Processing Photo :" + photo.PictureId + "\t" + photo.Picture + "\t");
                photoPath = pathSource + "\\" + photo.Picture;
                try
                {

                    if (File.Exists(photoPath))
                    {
                        FileStream fileStream = new FileStream(photoPath, FileMode.Open, FileAccess.Read);
                        PhotoInfo flickUpload = a.Upload(fileStream, photo);
                        fileStream.Close();
                        a.Save(photo, flickUpload);
                        sb.Append("Flickr Uploaded :" + flickUpload.PhotoId);
                    }
                    else
                    {
                        sb.Append("File does not exists!");
                    }

                }
                catch (FileNotFoundException ex)
                {
                    sb.Append(ex.Message + "\t");
                }
                catch (Exception ex)
                {
                    sb.Append(ex.Message + "\t");
                }
                finally
                {


                }
                Logger.log(sb.Append("\n").ToString());
                sb.Clear();
            }
          
            Logger.log(sb.Append("=============== Finished processing ===================").ToString());

        }
        private void button2_Click(object sender, EventArgs e)
        {
            label1.Text = "Processing ....";
            button2.Enabled = false;


            ProcessSyncFlickr();


            button2.Enabled = true;
            label1.Text = "Done!";
          
        }

        /// <summary>
        /// Process Avatar Picture
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            label1.Text = "Processing Avatar Picture ....";
            button3.Enabled = false;
            string photoPath;
            StringBuilder sb = new StringBuilder();

            PhotoManager a = new PhotoManager();
            List<AW_Staff> data = a.GetListPhotoStaff().ToList();

            foreach (AW_Staff photo in data)
            {
                sb.Append("Processing Avatar Photo :" + photo.UserName + "\t" + photo.Avatar + "\t");
                photoPath = pathAvatarSource + "\\" + photo.Avatar;
                try
                {

                    if (File.Exists(photoPath))
                    {
                        FileStream fileStream = new FileStream(photoPath, FileMode.Open, FileAccess.Read);
                        PhotoInfo flickUpload = a.UploadStaff(fileStream, photo);
                        fileStream.Close();
                        a.Save(photo, flickUpload);
                        sb.Append("Flickr Uploaded :" + flickUpload.PhotoId);
                    }
                    else
                    {
                        sb.Append("File does not exists!"); 
                    }

                }
                catch (FileNotFoundException ex)
                {
                    sb.Append(ex.Message + "\t");
                }
                catch (Exception ex)
                {
                    sb.Append(ex.Message + "\t");
                }
                finally
                {


                }
                Logger.log(sb.Append("\n").ToString());
                sb.Clear();
            }
            button3.Enabled = true;
            label1.Text = "Done!";
            Logger.log(sb.Append("=============== Finished processing ===================").ToString());

        }
          
            
        
    }
}
